package com.cysyz.mylib.utils;

import java.security.MessageDigest;

/**
 * 密码加密工具类 MD5加密
 */
public class PasswordUtil {
	// 定义md5加密密码的前缀
	public final static String MD5_PREFIX = "{md5}";

	/**
	 * md5加密方法
	 * 
	 * @param password
	 *            需要加密的字符串
	 * @return
	 */
	public static String encryption(String password) {
		if (password == null) {
			return password;
		}
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(password.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			// System.out.println("result: " + buf.toString());// 32 digit
			// System.out.println("result: " + buf.toString().substring(8,
			// 24));// 16 digit

			return buf.toString();

		} catch (Exception e) {
			return password;
		}
	}

	public static String encryption(String password, String salt) {
		return encryption(encryption(password)+ salt);
	}
	
	public static void main(String[] args) {
		String password = PasswordUtil.encryption("admin123");
		System.out.println(password);
	}
}
